package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.luck.picture.lib.entity.LocalMedia;
import com.mingtu.jgspatrol.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


public class MenuAdapter2 extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<LocalMedia> mData = new ArrayList<>();
    private LinkedHashMap<Integer, Object> hashMap = new LinkedHashMap<>();
    private int ImgSource[] = {R.mipmap.icon_menu_a, R.mipmap.icon_menu_b, R.mipmap.icon_menu_c, R.mipmap.icon_menu_d};
    private String menuText[] = {"文档资料库", "通讯录", "巡护记录", "上报记录"};
    private String menuText2[] = {"文档资料库", "通讯录"};

    private boolean isLeader;

    public MenuAdapter2(Context context, boolean isLeader) {
        this.context = context;
        this.isLeader = isLeader;
    }

    @Override
    public int getCount() {
        return isLeader ? 4 : 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_menu, null);

            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.iv_img);
            holder.tvText = (TextView) convertView.findViewById(R.id.tv_text);

            holder.layout.setOnClickListener(this);
            holder.layout.setTag(position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.ivImg.setBackgroundResource(ImgSource[position]);
        if (isLeader) {
            holder.tvText.setText(menuText[position]);
        } else {
            holder.tvText.setText(menuText2[position]);
        }


        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            try {
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新数据
     *
     * @param data
     */
    public void upData(List<LocalMedia> data) {
        for (int i = 0; i < data.size(); i++) {
            LocalMedia localMedia = data.get(i);
            mData.add(localMedia);
            hashMap.put(mData.size() - 1, localMedia);
        }
        notifyDataSetChanged();
    }


    public List<LocalMedia> getData() {
        return mData;
    }


    class ViewHolder {
        public LinearLayout layout;
        public ImageView ivImg;
        public TextView tvText;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
